package org.spiderflow.core.expression.parsing.ast.literal;

import org.spiderflow.core.expression.ExpressionTemplate;
import org.spiderflow.core.expression.ExpressionTemplateContext;
import org.spiderflow.core.expression.parsing.Span;
import org.spiderflow.core.expression.parsing.ast.Expression;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents a list literal of the form <code>[ value, value2, value3, ...]</code> which can be nested.
 */
public class ListLiteral extends Expression {
    public final List<Expression> values;

    public ListLiteral(Span span, List<Expression> values) {
        super(span);
        this.values = values;
    }

    public List<Expression> getValues() {
        return values;
    }

    @Override
    public Object evaluate(ExpressionTemplate template, ExpressionTemplateContext context) throws IOException {
        List<Object> list = new ArrayList<>();
        for (Expression value : values) {
            list.add(value.evaluate(template, context));
        }
        return list;
    }
}